﻿using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Updating;
using DevExpress.Xpo;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.BaseImpl;
using Microsoft.Extensions.DependencyInjection;
using XafDeleteActionByConditionInListView.Module.BusinessObjects;

namespace XafDeleteActionByConditionInListView.Module.DatabaseUpdate;

// For more typical usage scenarios, be sure to check out https://docs.devexpress.com/eXpressAppFramework/DevExpress.ExpressApp.Updating.ModuleUpdater
public class Updater : ModuleUpdater {
  public Updater(IObjectSpace objectSpace, Version currentDBVersion) :
      base(objectSpace, currentDBVersion) {
  }
  public override void UpdateDatabaseAfterUpdateSchema() {
    base.UpdateDatabaseAfterUpdateSchema();
    string[] bNames = ["张三", "李四", "MyName"];
    foreach (string bName in bNames) {
      TStudentBo bObject = ObjectSpace.FirstOrDefault<TStudentBo>(u => u.Name == bName);
      if (bObject == null) {
        bObject = ObjectSpace.CreateObject<TStudentBo>();
        bObject.Name = bName;
        bObject.Age = 10;
        bObject.Deleted = false;
      }
      else {
        bObject.Age = 10;
        bObject.Deleted = false;
      }
    }

    ObjectSpace.CommitChanges(); //Uncomment this line to persist created object(s).
  }
  public override void UpdateDatabaseBeforeUpdateSchema() {
    base.UpdateDatabaseBeforeUpdateSchema();
    //if(CurrentDBVersion < new Version("1.1.0.0") && CurrentDBVersion > new Version("0.0.0.0")) {
    //    RenameColumn("TStudentBoTable", "OldColumnName", "NewColumnName");
    //}
  }
}
